package com.hwn.datax.component;

import javafx.animation.RotateTransition;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.SVGPath;
import javafx.util.Duration;

public class StatusDisplay extends Pane {
    private SVGPath currentSVG;
    private RotateTransition rotateTransition;
    private final StringProperty statusProperty = new SimpleStringProperty();

    public StatusDisplay() {
        statusProperty.addListener((obs, oldStatus, newStatus) -> updateStatus(newStatus));
    }

    private void updateStatus(String status) {
        if (currentSVG != null) {
            getChildren().remove(currentSVG);
            if (rotateTransition != null) {
                rotateTransition.stop();
            }
        }
        switch (status) {
            case "loading" -> {
                currentSVG = createSVG(SVGStatus.LOADING_PATH);
                rotateTransition = createRotateTransition(currentSVG);
                currentSVG.setStroke(Color.web("#6495ED"));
                rotateTransition.play();
            }
            case "success" -> {
                currentSVG = createSVG(SVGStatus.SUCCESS_PATH);
                currentSVG.setStroke(Color.web("#32CD32"));
            }
            case "error" -> {
                currentSVG = createSVG(SVGStatus.ERROR_PATH);
                currentSVG.setStroke(Color.web("#F56C6C"));
            }
            default -> {
                return; // 如果状态未知，则不显示任何东西
            }
        }
        currentSVG.setStrokeWidth(4);
        currentSVG.setFill(Color.TRANSPARENT);

        getChildren().add(currentSVG);
    }

    public StringProperty statusProperty() {
        return statusProperty;
    }

    public SVGPath createSVG(String svgPath) {
        SVGPath svg = new SVGPath();
        svg.setContent(svgPath);
        return svg;
    }

    public RotateTransition createRotateTransition(SVGPath svg) {
        RotateTransition rt = new RotateTransition(Duration.seconds(2), svg);
        rt.setByAngle(360);
        rt.setCycleCount(RotateTransition.INDEFINITE);
        return rt;
    }

    static class SVGStatus {
        public static final String LOADING_PATH = "M24 4V8 M34 6.6795L32 10.1436 M41.3203 14L37.8562 16 M44 24H40 M41.3203 34L37.8562 32 M34 41.3205L32 37.8564 M24 44V40 M14 41.3205L16 37.8564 M6.67969 34L10.1438 32 M4 24H8 M6.67969 14L10.1438 16 M14 6.6795L16 10.1436";
        public static final String SUCCESS_PATH = "M24 4L29.2533 7.83204L35.7557 7.81966L37.7533 14.0077L43.0211 17.8197L41 24L43.0211 30.1803L37.7533 33.9923L35.7557 40.1803L29.2533 40.168L24 44L18.7467 40.168L12.2443 40.1803L10.2467 33.9923L4.97887 30.1803L7 24L4.97887 17.8197L10.2467 14.0077L12.2443 7.81966L18.7467 7.83204L24 4Z M17 24L22 29L32 19";
        public static final String ERROR_PATH = "M24 5L2 43H46L24 5Z M24 35V36 M24 19.0005L24.0083 29";
    }
}